package com.example.yckjbigdataservice.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.io.IOException;
import java.nio.file.Paths;

@Configuration
@Slf4j
@Order(Ordered.LOWEST_PRECEDENCE)
public class HdfsConfig {

    @Value("${custom.hdfs.principal}")
    private String principal;

    @Value("${custom.hdfs.user-keytab.file.path}")
    private String userKeytabFilePath;

    @Value("${custom.hdfs.kbr5.file.path}")
    private String kbr5FilePath;

    @Value("${custom.hdfs.hadoop.config.path}")
    private String configPath;

    @Bean
    public org.apache.hadoop.conf.Configuration confHdfsLoad() throws IOException {
        System.setProperty("java.security.krb5.conf", kbr5FilePath);
        org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
        // conf file
        conf.addResource(new Path(Paths.get(configPath, "hdfs-site.xml").toAbsolutePath().toString()));
        conf.addResource(new Path(Paths.get(configPath, "core-site.xml").toAbsolutePath().toString()));
        conf.addResource(new Path(Paths.get(configPath, "hbase-site.xml").toAbsolutePath().toString()));
        return conf;
    }
}
